package com.ztesoft.huangfu.learning.springboot.es.controller;

import com.alibaba.fastjson.JSON;
import com.ztesoft.huangfu.learning.springboot.es.document.StudentDoc;
import com.ztesoft.huangfu.learning.springboot.es.document.StudentDocBuilder;
import com.ztesoft.huangfu.learning.springboot.es.service.EsAggregationsSearch;
import com.ztesoft.huangfu.learning.springboot.es.service.EsComplexSearchSearch;
import com.ztesoft.huangfu.learning.springboot.es.service.EsSearchServiceBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/12/21
 * Time: 14:27
 * Version: 1.0
 * Description:
 **/
@RestController
public class EsSearchController {

    @Autowired
    EsAggregationsSearch esAggregationsSearch;

    @Autowired
    EsComplexSearchSearch esComplexSearchSearch;

    @Autowired
    EsSearchServiceBase esSearchServiceBase;


    /**
     * 插入
     *
     * @throws ParseException
     */
    @GetMapping("/save")
    public String save() throws ParseException {
        List studentList = new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        StudentDoc studentDoc1 = StudentDocBuilder.create()
                .addId("001")
                .addName("宋江")
                .addAge(37)
                .addBirthday(dateFormat.parse("1073-04-20T11:22:45.000").getTime())
                .addIsInSchool(true)
                .addSex("男")
                .addTelNo(15289876765L)
                .addHomeTown("山东省菏泽市郓城县")
                .addDescribe("宋江（1073~1124），字公明，绰号呼保义、及时雨、孝义黑三郎，施耐庵所作古典名著《水浒传》中的人物。")
                .builder();
        studentList.add(studentDoc1);
        esSearchServiceBase.save(studentList);
        return JSON.toJSONString(studentList);
    }

    /**
     * 根据indexName删除整个索引分片
     */
    @GetMapping("/deleteIndex")
    public String deleteIndex(String indexName) {
        esComplexSearchSearch.deleteIndex(indexName);
        return "删除成功！";
    }

    /**
     * 根据Id查询索引
     *
     * @param id
     * @return
     */
    @GetMapping("/queryById")
    public String queryById(String id) {
        return JSON.toJSONString(esSearchServiceBase.getById(id));
    }

    /**
     * 查询所有索引
     *
     * @return
     */
    @GetMapping("/queryAll")
    public String queryAll() {
        return JSON.toJSONString(esSearchServiceBase.getAll());
    }

    /**
     * 根据Id删除索引
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public String deleteById(String id) {
        esSearchServiceBase.delete(id);
        return "删除成功！";
    }

    /**
     * 删除所有索引
     */
    @GetMapping("/deleteAll")
    public String deleteAll() {
        esSearchServiceBase.deleteAll();
        return "删除成功！";
    }
    /**
     * 根据条件删除
     */
    @GetMapping("/deleteByCondition")
    public String deleteByCondition(String deleteCondition) {
        esSearchServiceBase.deleteByCondition(deleteCondition);
        return "删除成功！";
    }

    //关键字查询
    @GetMapping("/queryByKeyword")
    public String queryByKeyword(String keyword) {
        return JSON.toJSONString(esComplexSearchSearch.query(keyword, StudentDoc.class));
    }

    //分页查询
    @GetMapping("/queryByPage")
    public String queryByPage(String keyword, String pageNo, String pageSize) {
        return JSON.toJSONString(esComplexSearchSearch.queryByPage(keyword, StudentDoc.class, Integer.valueOf(pageNo), Integer.valueOf(pageSize)));
    }

    //高亮查询
    @GetMapping("/queryHitByPage")
    public String queryHitByPage(String keyword, String pageNo, String pageSize, String indexName, String name) {
        return JSON.toJSONString(esComplexSearchSearch.queryHitByPage(Integer.valueOf(pageNo), Integer.valueOf(pageSize), keyword, indexName, name));
    }

    //聚合查询
    @GetMapping("/calculation")
    public String calculation() {
        return JSON.toJSONString(esAggregationsSearch.calculation());
    }
}
